package com.cy.pj.goods.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DefaultGoodsDao implements GoodsDao {
    @Autowired
    private DataSource dataSource;
    private static final Logger log=
             LoggerFactory.getLogger(DefaultGoodsDao.class);
    @Override
    public List<Map<String, Object>> findGoods() throws SQLException {
        log.error("查询错误,{}","错误原因");
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        String sql="select * from tb_goods";
        conn = dataSource.getConnection();
        stmt =conn.createStatement();
        rs = stmt.executeQuery(sql);
        List<Map<String,Object>> list =new ArrayList<>();
        while (rs.next()){
            Map<String,Object>map =new HashMap<>();
            map.put("id",rs.getInt("id"));
            map.put("name",rs.getString("name"));
            map.put("remark",rs.getString("remark"));
            map.put("createdTime",rs.getString("createdTime"));
            list.add(map);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }
}
